<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>

    </style>
</head>

<body>
    <script>


        // 使用工厂函数
        // 创建饮水机对象，饮水机对象有热水和冷水方法，以及饮水机位置属性

        function waterDispenser() {
            var obj = new Object();
            obj.hotWater = hotWater;
            obj.coldWater = coldWater;
            obj.position = position;
            return obj;
        }

        function hotWater() {
            console.log('热水');
        }
        function coldWater() {
            console.log('冷水');
        }
        function position() {
            console.log('厨房');
        }

        var waterDispenserObject = waterDispenser();
        console.log(waterDispenserObject);
        waterDispenserObject.hotWater();//热水


        // 创建三个怪物对象，并且输出怪物的名字、性别、等级，怪物执行攻击方法，输出“水球术”

        function monster(name, sex, grade) {
            var obj = new Object();
            obj.name = name;
            obj.sex = sex;
            obj.grade = grade;
            obj.attack = attack;
            return obj;
        }
        function attack() {
            return '水球术';
        }
        var monster01 = monster('低级怪', '雄', '5');
        var monster02 = monster('中级怪', '雌', '10');
        var monster03 = monster('精英怪', '雄', '20');

        console.log('怪物1的名字为：' + monster01.name + '  性别为：' + monster01.sex + '  等级为：' + monster01.grade + '  攻击方式为：' + monster01.attack());
        console.log('怪物2的名字为：' + monster02.name + '  性别为：' + monster02.sex + '  等级为：' + monster02.grade + '  攻击方式为：' + monster02.attack());
        console.log('怪物3的名字为：' + monster03.name + '  性别为：' + monster03.sex + '  等级为：' + monster03.grade + '  攻击方式为：' + monster03.attack());

        // 创建一个动漫对象，输出动漫的人物，爱好，性别，能力
        function createPerson(name, hobby, sex, callback) {
            var obj = new Object();
            obj.name = name;
            obj.hobby = hobby;
            obj.sex = sex;
            obj.skill = callback;
            return obj;
        }

        var naruto01 = createPerson('旗木卡卡西', '亲热天堂', '男', function () {
            alert('千年杀');
        });

        var naruto02 = createPerson('旋涡鸣人', '一乐拉面', '男', function () {
            document.write('色诱术');
        });
        // console.log(naruto01);
        naruto01.skill();
        naruto02.skill();



        // - 以上是某班级一次考试的成绩表。请计算每个学生总成绩，并按总成绩排名。统计各单科成绩第一名，输出其成绩与学号。
        // - 并根据总成绩计算每个学生成绩备注保存在 Comment 中，60分以上为D，70分以上为C，80分以上为B，90分以上为A，60分以下为E

        var data = [
            { StudentNumber: 2022003, Chinese: 86, Math: 64, English: 80, TotalScore: "", Comment: "" },
            { StudentNumber: 2022004, Chinese: 78, Math: 99, English: 91, TotalScore: "", Comment: "" },
            { StudentNumber: 2022005, Chinese: 107.5, Math: 97, English: 70, TotalScore: "", Comment: "" },
            { StudentNumber: 2022006, Chinese: 112, Math: 61, English: 92, TotalScore: "", Comment: "" },
            { StudentNumber: 2022007, Chinese: 101, Math: 79, English: 104, TotalScore: "", Comment: "" },
            { StudentNumber: 2022008, Chinese: 71, Math: 72, English: 105, TotalScore: "", Comment: "" },
            { StudentNumber: 2022009, Chinese: 56, Math: 68, English: 61, TotalScore: "", Comment: "" },
            { StudentNumber: 2022010, Chinese: 98, Math: 83, English: 77, TotalScore: "", Comment: "" }
        ];
        // achievement(data)

        // function achievement(data) {
        //     var sum = 0;
        //     var sumArr = [];
        //     var maxChinese = 0;
        //     var maxChineseIndex = 0;
        //     var maxMath = 0;
        //     var maxMathIndex = 0;
        //     var maxEnglish = 0;
        //     var maxEnglishIndex = 0;
        //     for (let i = 0; i < data.length; i++) {
        //         sum = data[i].Chinese + data[i].Math + data[i].English
        //         data[i].TotalScore = sum;
        //         if ((data[i].TotalScore / 3) >= 90) {
        //             data[i].Comment = 'A';
        //         } else if (data[i].TotalScore / 3 >= 80) {
        //             data[i].Comment = 'B';
        //         } else if (data[i].TotalScore / 3 >= 70) {
        //             data[i].Comment = 'C';
        //         } else if (data[i].TotalScore / 3 >= 60) {
        //             data[i].Comment = 'D';
        //         } else {
        //             data[i].Comment = 'E';
        //         }
        //     }

        //     for (var i = 0; i < data.length - 1; i++) {
        //         for (var j = 0; j < data.length - i - 1; j++) {
        //             if (data[j].TotalScore < data[j + 1].TotalScore) {
        //                 [data[j], data[j + 1]] = [data[j + 1], data[j]];
        //             }
        //         }
        //     }

        //     for (let i = 0; i < data.length; i++) {
        //         if (data[i].Chinese > maxChinese) {
        //             maxChinese = data[i].Chinese;
        //             maxChineseIndex = i;
        //         }
        //         if (data[i].Math > maxMath) {
        //             maxMath = data[i].Math;
        //             maxMathIndex = i;
        //         }
        //         if (data[i].English > maxEnglish) {
        //             maxEnglish = data[i].English;
        //             maxEnglishIndex = i;
        //         }

        //     }

        //     console.log('语文单科成绩第一为： ' + maxChinese + '学号为： ' + data[maxChineseIndex].StudentNumber);
        //     console.log('数学单科成绩第一为： ' + maxMath + '学号为： ' + data[maxMathIndex].StudentNumber);
        //     console.log('英语单科成绩第一为： ' + maxEnglish + '学号为： ' + data[maxEnglishIndex].StudentNumber);
        //     console.log(data);
        // }

        // 构造函数
        function Achievement(data) {
            this.data = data;
        }

        Achievement.prototype.outputScore = function () {
            let data = this.data;
            for (let stu of data) {
                stu.TotalScore = stu.Chinese + stu.Math + stu.English;
                var stuscore = (stu.TotalScore / 3).toFixed(1);
                if (stuscore >= 110) {
                    stu.Comment = "A";
                } else if (stuscore >= 100) {
                    stu.Comment = "B";
                } else if (stuscore >= 90) {
                    stu.Comment = "C";
                } else if (stuscore >= 72) {
                    stu.Comment = "D";
                } else {
                    stu.Comment = "E";
                }
            }
            // console.log(this.data);
            // console.log(data);
            this.scoreSort(data, 'Chinese', '语文');
            this.scoreSort(data, 'Math', '数学');
            this.scoreSort(data, 'English', '英语');
            this.scoreSort(data, 'TotalScore', '总成绩');
        }

        Achievement.prototype.scoreSort = function (data, subject, subjectName) {
            data.sort(function (a, b) {
                // console.log(a, b);
                return b[subject] - a[subject];
            })
            document.write(subjectName + '第一名是：' + data[0].StudentNumber + '； 成绩值为：' + data[0][subject]);
            document.write('<br>');
        }

        var score = new Achievement(data);
        score.outputScore();
        console.log(data);
        console.log(score);



        // 创建一个电脑对象，有颜色(color)、重量(weight)、品牌(brand)、型号(type)等属性，有看电影(watch)、听音乐(listen)、打游戏(play)和敲代码(coding)等方法。
        function Computer(color, weight, brand, type) {
            this.color = color;
            this.weight = weight;
            this.brand = brand;
            this.type = type;
        }

        Computer.prototype.watch = function () {
            console.log('我在看电影');
        }
        Computer.prototype.listen = function () {
            console.log('我在听音乐');
        }
        Computer.prototype.play = function () {
            console.log('我在打游戏');
        }
        Computer.prototype.coding = function () {
            console.log('我在敲代码');
        }


        var computer = new Computer('银白色', '1KG', 'HP', 'Elite蜻');
        console.log(computer);
        computer.watch();
        computer.listen();
        computer.play();
        computer.coding();


    </script>
</body>

</html>